
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import home from '../views/Home.vue'
const routes = [
    {
        path: '/',
        name: 'home',
        component: home
    },
    {
        path: '/find',
        name: 'find',
        component: () => import('../views/Find.vue'),
        children: [
            {
                path: 'toplist',
                name: 'toplist',
                component: () => import('../views/discover/Toplist.vue'),
                //三级路由的配置
                // children: [
                //     {}
                // ]
            },
            {
                path: 'playlist',
                name: 'playlist',
                component: () => import('../views/discover/Playlist.vue')
            }
            ,
            {
                path: 'djradio',
                name: 'djradio',
                component: () => import('../views/discover/Djradio.vue')
            }
            ,
            {
                path: 'album',
                name: 'album',
                component: () => import('../views/discover/Album.vue')
            }
        ]
    },
    {
        path: '/my',
        name: 'my',
        component: () => import('../views/My.vue')
    },
    {
        path: '/friend',
        name: 'friend',
        component: () => import('../views/Friend.vue')
    },
    {
        path: '/parent',
        name: 'parent',
        component: () => import('../views/Parent.vue')
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/parent/:username',
        name:'parent',
        component: () => import('../views/Parent.vue')
    },
]

const router = createRouter({
    //createWebHashHistory() 这是hash模式  包含#的 
    history: createWebHistory(),
    routes
})

const isAuthenticated= false
router.beforeEach((to, from, next) => {
    // if (to.path !== '/login' && !isAuthenticated) {
    //     alert("请登录")
    //     next(true)
    // } else {
    //     next({name:'login'})
    // }
    if (to.name == 'my' && !isAuthenticated){
        next({ name: 'login' })
     //是否进入和拒绝进入
    }
    next()


    // if (to.name !== 'login' && !isAuthenticated) next({ name: 'Login' })
    // // 如果用户未能验证身份，则 `next` 会被调用两次
    // // alert("请登录")
    // next()
})

//默认导出 让别的组件应用 main.js 要全局挂载
export default router